<?php
require_once("page.class.php");
require_once("db_functions.php");
require_once("functions.php");


class upload extends page {
	var $db;
	
	var $logfile;
	var $datadir="logs";
	var $data=array();
	
	var $state_mask=array(
		"1" => "novi",
		"2" => "izmena",
		"3" => "brisanje"
	);
	
	function upload(){
		$this->db = new db_functions();
		$action = getFromInput("a");
		
		switch ($action){
			default:
				$this->showStartForm();
				break;
		}
	}
	
	function showStartForm(){
		
		$od = getFromInput("od");
		$do = getFromInput("do");
		
		if(isset($od) && isset($do)){
			list($ids,$names,$states,$classes,$updata) = $this->getChanges($od,$do);
			if(count($this->data)>0){
				$this->assign("id",$ids);
				$this->assign("name",$names);
				$this->assign("state",$states);
				$this->assign("class_web",$classes);
				$this->assign("info",count($ids));
				$this->assign("updata",$updata);
			}else{
				$this->assign("comment_start","<!--");
				$this->assign("comment_end","-->");
			}
		}else{
			$this->assign("comment_start","<!--");
			$this->assign("comment_end","-->");
		}
		
		$this->assign("date_od",date("d.m.Y",time()-(60*60*24)));
		$this->assign("date_do",date("d.m.Y"));
		echo $this->parseTemplate("upload.tpl");
	}
	
	function getChanges($od,$do){
		$this->loadChanges($od,$do);
		
		foreach ($this->data as $table=>$v){
			foreach ($v as $attribute=>$m){
				foreach ($m as $value=>$n){
					$last_change = $n[max(array_keys($n))];
					if($table == "komitenti"){
						$name = $this->db->getOne("SELECT komitent_name FROM komitenti WHERE komitent_id=$value LIMIT 1");
						$class = "";
					}elseif($table == "komitenti_delatnosti"){
						$row = $this->db->getRow("SELECT komitent_id,komitent_name FROM komitenti, komitenti_delatnosti WHERE komdel_kom_id=komitent_id AND komdel_id=$value LIMIT 1");
						$value = $row[0];
						$name = $row[1];
						$class = "web";
					}
					$upd = "$table{!}$attribute{!}$value{!}".$last_change["state"];
					
					$ids[] = $value;
					$names[] = $name?$name:"<span style=\"color:red\">obrisano</span>";
					$states[] = $this->state_mask[$last_change["state"]];
					$classes[] = $class;
					$updata[] = $upd;
				}
			}
		}
		return array($ids,$names,$states,$classes,$updata);
	}
	
	function loadChanges($od,$do){
		$dir = opendir($this->datadir);
		
		$od = date2int($od);
		$do = date2int($do);
		
		while ($f = readdir($dir)) {
			if(($f!=".") && ($f!="..")){
				$fd = date2int(substr($f,0,2).".".substr($f,2,2).".".substr($f,4));
				if($fd >= $od && $fd <= $do){
					$file = file($this->datadir."/".$f);
					foreach ($file as $line){
						$items = explode("\t",$line);
						$user	= $items[0];
						$time	= $items[1];
						$ip		= $items[2];
						$state	= $items[3];
						$table	= $items[4];
						$attr	= $items[5];
						$value	= trim($items[6]);
						
						$this->data[$table][$attr][$value][$time] = array(
							"user"	=> $user,
							"ip"	=> $ip,
							"state"	=> $state
						);
					}
				}
			}
		}
	}
}

?>